<?php

namespace App\Service\statistics;
use App\Model\Applet;
use App\Model\AppletPlayletRel;
use App\Model\PlayletBind;
use App\Model\PlayletEpisodes;
use App\Service\CommicService;
use App\Model\PlayletTag;
use App\Model\Playlet;
use App\Service\queue\DramaService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
use Hyperf\DbConnection\Db;

class PlayletBindService extends CommicService
{
    const cacheDuration = 60 * 15;

    #[Inject]
    private DramaService $dramaService;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    const STATUS_ENABLE  = 1;

    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;

        $query = PlayletBind::query()->select(['id','playlet_id','amount','updated_at','created_at']);
        if ($keyword) {

           $playletIds =  Playlet::query()->Where([['name', 'like', '%'.$keyword.'%']])->pluck('id');

        }

        if(!empty($playletIds)){
            $query->whereIn('playlet_id',$playletIds);
        }
        $models = $query->orderBy('id','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();

            if($item->playlet){
                $data['playlet_name']  = $item->playlet->name;
            }else{
                $data['playlet_name']  = '';
            }
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function createOrUpdate(Array $postData, bool $is_update = false, PlayletBind $playletBindInfo = null){

        $playlet_id = $postData['playlet_id'];
        $amount = $postData['amount'] ?? 0;
        $model = !empty($playletBindInfo)? $playletBindInfo : new PlayletBind();

        Db::beginTransaction();
        try {
            $playletInfo = Playlet::query()->where('id',$playlet_id)->first();
            if(empty($playletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $is_check_playlet = true;
            if($is_update) {
                //修改时判断用户名是否一致
                if ($playlet_id == $model->playlet_id) {
                    $is_check_playlet = false;
                }
            }
            if($is_check_playlet){
                $playletBindInfo = PlayletBind::query()->where('playlet_id',$playlet_id)->first();
                if(!empty($playletBindInfo)){
                    return $this->response(SystemCode::SYSTEM_ERROR, '已存在此短剧数据');
                }
                $model->playlet_id = $playlet_id;
            }
            if($amount > 100){
                return $this->response(SystemCode::SYSTEM_ERROR, '比例不能大于100');
            }
            if($amount <= 0){
                return $this->response(SystemCode::SYSTEM_ERROR, '比例设置不能为0');
            }
            $amount = $amount*100;
            if($amount < 0){
                return $this->response(SystemCode::SYSTEM_ERROR, '比例设置错误');
            }
            $model->amount = $amount/100;

            if($model->save()){
                Db::commit();
                return $this->response(SystemCode::SYSTEM_SUCCESS);
            }else{
                Db::rollBack();
                return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
            }

        }catch (\Exception $e){
            Db::rollBack();
            return $this->response(SystemCode::SYSTEM_ERROR,$e->getMessage());
        }
    }
}